/*
 * @Author: hc
 * @Date: 2024-03-05 10:34:26
 * @LastEditTime: 2024-03-05 21:19:11
 * @LastEditors: hc
 * @Description:
 */
import { useHistoryTravel, useMemoizedFn } from 'ahooks';
import { Input } from 'antd';
import { forwardRef, useImperativeHandle, useRef } from 'react';
import { flushSync } from 'react-dom';
import { getCursorPosition, setCursorPosition } from "./utils";
import { jsx as _jsx } from "react/jsx-runtime";
var TextareaEditor = /*#__PURE__*/forwardRef(function (_, ref) {
  var _useHistoryTravel = useHistoryTravel(''),
    value = _useHistoryTravel.value,
    setValue = _useHistoryTravel.setValue,
    back = _useHistoryTravel.back,
    forward = _useHistoryTravel.forward,
    reset = _useHistoryTravel.reset;
  var textAreaRef = useRef();
  var cursorRef = useRef({
    start: 0,
    end: 0
  });

  /**
   * 记录光标位置
   */
  var handleRecordCursorPosition = function handleRecordCursorPosition() {
    var position = getCursorPosition(textAreaRef.current);
    cursorRef.current = position;
  };

  /**
   * 值变动
   */
  var handleOnChange = function handleOnChange(e) {
    setValue(e.target.value);
    handleRecordCursorPosition();
  };

  /**
   * 文本插入
   */
  var insertEdit = useMemoizedFn(function (_ref) {
    var text = _ref.text,
      type = _ref.type;
    var newText = '';
    var insertStart = cursorRef.current.start;
    var insertEnd = cursorRef.current.end + text.length + 1;
    if (type === 'field') {
      var _value$slice, _value$slice2;
      newText = "".concat((_value$slice = value === null || value === void 0 ? void 0 : value.slice(0, insertStart)) !== null && _value$slice !== void 0 ? _value$slice : '').concat(text, " ").concat((_value$slice2 = value === null || value === void 0 ? void 0 : value.slice(insertStart)) !== null && _value$slice2 !== void 0 ? _value$slice2 : '');
    } else {
      var _value$slice3, _value$slice4;
      newText = "".concat((_value$slice3 = value === null || value === void 0 ? void 0 : value.slice(0, insertStart)) !== null && _value$slice3 !== void 0 ? _value$slice3 : '').concat(text, "()").concat((_value$slice4 = value === null || value === void 0 ? void 0 : value.slice(insertStart)) !== null && _value$slice4 !== void 0 ? _value$slice4 : '');
    }
    setValue(newText);
    setTimeout(function () {
      return flushSync(function () {
        return setCursorPosition(textAreaRef.current, insertEnd, insertEnd);
      });
    }, 50);
  });

  /**
   * 获取已输入文本
   */
  var getHtmlValue = function getHtmlValue() {
    return value !== null && value !== void 0 ? value : '';
  };

  /**
   * 设置输入文本
   */
  var setDefaultHtml = function setDefaultHtml(str) {
    setValue(str !== null && str !== void 0 ? str : '');
  };
  useImperativeHandle(ref, function () {
    return {
      insertEdit: insertEdit,
      getHtmlValue: getHtmlValue,
      setDefaultHtml: setDefaultHtml,
      back: back,
      forward: forward,
      reset: reset
    };
  });
  return /*#__PURE__*/_jsx(Input.TextArea, {
    style: {
      height: '100%'
    },
    ref: textAreaRef,
    placeholder: "\u8BF7\u8F93\u5165\u6587\u672C/\u63D2\u5165\u51FD\u6570\u6216\u53D8\u91CF...",
    value: value,
    onChange: handleOnChange,
    onBlur: handleRecordCursorPosition
  });
});
export default TextareaEditor;